using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LAFDC.DataAccess;
using LAFDC.Logging;

namespace LAFDC.Web.UI {
    public partial class GetAdherent : System.Web.UI.Page {
        private string adh_previd;
        private static readonly string source = "AdhConsultation.aspx";

        protected void Page_Load(object sender, EventArgs e) {
            if (this.Request.Params.Count > 0) {
                adh_previd= this.Request.QueryString.Get("adh");
                txtPREV_ID.Text = adh_previd;

                if ((adh_previd == null) || ( adh_previd.Length == 0)) {
                    FDCLogger.LogEvent("AdhConsultation a ete apeller sans argument adh.", source, 2);
                }
                else {
                    FDCLogger.LogEvent("AdhConsultation a ete apeller avec argument adh " + adh_previd, source, 3);
                    imgEdit.NavigateUrl = "AdhEdition.aspx?adh=" + adh_previd;
                    imgAdd.NavigateUrl = "AdhAjout.aspx";
                    GenerateClientScript(adh_previd);
                    LoadData(adh_previd);

                }
            }
        }

        private void GenerateClientScript(string adhid) {
            Page.ClientScript.RegisterClientScriptInclude("FamilleEdition.aspx.js", "/LAFDCWeb/FamilleEdition.aspx.js");

            StringBuilder sb = new StringBuilder();

            sb.Append("<script type=\"text/javascript\" language=\"javascript\">\n");
            sb.AppendFormat("var Adh = \"{0}\";\n", adhid);
            sb.Append("</script>\n");

            Page.ClientScript.RegisterClientScriptBlock(GetType(), "open_win", sb.ToString());
        }


        private void LoadData(string adhid) {
            FDCLogger.LogEvent("La page est en train de charger les informations de la base de donne", source, 4);
            ConnectionManager cn = new ConnectionManager();
            SqlConnection conn = cn.GetSqlConnection();
            SqlCommand tsql = conn.CreateCommand();

            #region// ETAT CIVILE
            tsql.CommandType = CommandType.StoredProcedure;
            tsql.CommandText = "spGetETAT_CIVILE_byPrevID";
            SqlParameter param = tsql.Parameters.Add("@PREV_ID", SqlDbType.NVarChar);
            param.Value = adhid;
            conn.Open();

            using (SqlDataReader reader = tsql.ExecuteReader()) {
                while (reader.Read()) {
                    if (reader.HasRows) {
                        FDCLogger.LogEvent("La page est en train de charger les informations d'etat civile", source, 4);
                        txtPREV_ID.Text = reader.GetValue(0).ToString();
                        txtNOM.Text = reader.GetValue(1).ToString();
                        txtPRENOM.Text = reader.GetValue(2).ToString();
                        txtINTITULE.Text = reader.GetValue(3).ToString();
                        txtSITUATION_FAM.Text = reader.GetValue(4).ToString();
                        txtANNIV.Text = reader.GetValue(5).ToString();
                    }
                }
            }
            conn.Close();
            #endregion
            #region// ADRESSE
            tsql.CommandText = "spGetADRESSE_byPrevID";
            conn.Open();

            using (SqlDataReader reader = tsql.ExecuteReader()) {
                while (reader.Read()) {
                    if (reader.HasRows) {
                        FDCLogger.LogEvent("La page est en train de charger les informations d'adresse", source, 4);
                        txtAdr1.Text = reader.GetString(0);
                        txtAdr2.Text = reader.GetString(1);
                        txtCodePostal.Text = reader.GetString(2);
                        txtVille.Text = reader.GetString(3);
                    }
                }
            }
            conn.Close();
            #endregion
            #region// STATUS
            tsql.CommandText = "spGetSTATUS_byPrevID";
            conn.Open();
            using (SqlDataReader reader = tsql.ExecuteReader()) {
                while (reader.Read()) {
                    if (reader.HasRows) {
                        FDCLogger.LogEvent("La page est en train de charger les informations de status", source, 4);
                        txtAD.Text = reader.GetString(0);
                        txtSECTION.Text = reader.GetString(1);

                        if (reader.GetString(2) == "OUI") {
                            chkRETENU.Checked = true;
                        }
                        // txtRUB2.Text = reader.GetString(3);
                        if (!reader.IsDBNull(4)) {
                            txtIRET.Text = reader.GetString(4);
                        }
                        if (!reader.IsDBNull(5)) {
                            txtOTIG.Text = reader.GetString(5);
                        }
                        txtADHESION.Text = reader.GetString(6);
                        if (!reader.IsDBNull(7)) {
                            txtAFFECTATION.Text = reader.GetString(7);
                        }
                    }
                }
            }
            conn.Close();
            #endregion
            #region//CONTACT

            tsql.CommandText = "spGetCONTACT_byPrevID";
            conn.Open();

            bool headerSet = false;

            using (SqlDataReader reader = tsql.ExecuteReader()) {
                while (reader.Read()) {
                    if (reader.HasRows) {
                        FDCLogger.LogEvent("La page est en train de charger les informations de contact", source, 4);

                        if (!headerSet) {
                            #region //SET TABLE HEADER
                            TableRow head = new TableRow();
                            head.Height = 30;
                            head.BackColor = Color.White;

                            TableCell cell = new TableCell();
                            cell.Controls.Add(new LiteralControl("Ref# "));
                            cell.Width = 70;
                            head.Cells.Add(cell);

                            cell = new TableCell();
                            cell.Controls.Add(new LiteralControl("Valeur"));
                            cell.Width = 90;
                            head.Cells.Add(cell);

                            cell = new TableCell();
                            cell.Controls.Add(new LiteralControl("Type"));
                            cell.Width = 90;
                            head.Cells.Add(cell);

                            cell = new TableCell();
                            cell.Controls.Add(new LiteralControl("Description"));
                            cell.Width = 150;
                            head.Cells.Add(cell);

                            tblContact2.Rows.Add(head);
                            #endregion
                            headerSet = true;
                        }

                        TableRow r = new TableRow();
                        r.Height = 30;

                        for (int i = 0; i < 4; i++) {
                            TableCell c = new TableCell();
                            c.Controls.Add(new LiteralControl(reader.GetValue(i).ToString()));
                            r.Cells.Add(c);
                        }
                        tblContact2.Rows.Add(r);
                    }
                }
            }
            conn.Close();
            #endregion
            #region //FAMILLE
            tsql.CommandText = "spGetFAMILLE_byPrevID";
            conn.Open();

            headerSet = false;

            using (SqlDataReader reader = tsql.ExecuteReader()) {
                while (reader.Read()) {
                    if (reader.HasRows) {
                        FDCLogger.LogEvent("La page est en train de charger les informations de famille", source, 4);

                        if (!headerSet) {
                            #region //SET TABLE HEADER
                            TableRow head = new TableRow();
                            head.Height = 30;
                            head.BackColor = Color.White;

                            TableCell cell = new TableCell();
                            cell.Controls.Add(new LiteralControl("Ref #"));
                            cell.Width = 70;
                            head.Cells.Add(cell);

                            cell = new TableCell();
                            cell.Controls.Add(new LiteralControl("Position"));
                            cell.Width = 70;
                            head.Cells.Add(cell);

                            cell = new TableCell();
                            cell.Controls.Add(new LiteralControl("Nom"));
                            cell.Width = 80;
                            head.Cells.Add(cell);

                            cell = new TableCell();
                            cell.Controls.Add(new LiteralControl("Prenom"));
                            cell.Width = 80;
                            head.Cells.Add(cell);

                            cell = new TableCell();
                            cell.Controls.Add(new LiteralControl("Sexe"));
                            cell.Width = 70;
                            head.Cells.Add(cell);

                            cell = new TableCell();
                            cell.Controls.Add(new LiteralControl("Anniv."));
                            cell.Width = 90;
                            head.Cells.Add(cell);

                            tblFamille.Rows.Add(head);
                            #endregion
                            headerSet = true;
                        }

                        TableRow r = new TableRow();
                        r.Height = 30;

                        for (int i = 0; i < reader.FieldCount; i++) {
                            TableCell c = new TableCell();
                            c.Controls.Add(new LiteralControl(reader.GetValue(i).ToString()));
                            c.Width = 70;
                            r.Cells.Add(c);
                        }
                        tblFamille.Rows.Add(r);
                    }
                }
            }
            #endregion
        }
    }
}
